package com.haizheng.sockte;

import java.io.*;
import java.net.Socket;

/**
 * @program: Demo
 * @description: com.haizheng.sockte
 * @author: Hz~
 * @create: 2019-02-18 11:05
 **/
public class SocketClient {

    public static void main(String[] args) throws IOException {
        Socket socket = null;
        InputStream is = null;
        OutputStream ops = null;
        BufferedReader reader = null;
        try {
            socket = new Socket("www.itcast.cn", 80);
            is = socket.getInputStream();
            ops = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(ops);
            BufferedWriter bw = new BufferedWriter(osw);
            //编辑请求头，也就是请求参数，这是最基本的 正常请求会带好多参数http://
            bw.write("GET /subject/javaeezly/ HTTP/1.1\r\n");
            bw.write("Connection: Keep-Alive\r\n");
            bw.write("Host: www.itcast.cn\r\n");
            bw.write("Connection: Keep-Alive\r\n\r\n");
            bw.flush();
            reader = new BufferedReader(new InputStreamReader(is));
            String str = null;
            while ((str = reader.readLine()) != null) {
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != socket) {
                socket.close();
            }
            if (null != is) {
                ops.close();
            }
            if (null != ops) {
                ops.close();
            }
            if (null != reader) {
                reader.close();
            }
        }
    }
}
